﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using DevExpress.Web.ASPxEditors;
using System.Web.Services;

public partial class MemberInfo_MemberEdit : Frame.Pages.ActionInfomationEditBasePage
{
    protected override string ActionID
    {
        get { return "6001"; }
    }

    protected override string UserFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return "IUDQ";
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? CMSUser.GetFunction(ActionID).op : "";
                }
            }
            else
            {
                return "";
            }
        }
    }

    protected override bool IsShowSpecialFunctions
    {
        get
        {
            if (CMSUser.Current.IsLogin)
            {
                if (CMSUser.Current.IsAdmin)
                {
                    return true;
                }
                else
                {
                    return CMSUser.GetFunction(ActionID) != null ? (CMSUser.GetFunction(ActionID).op_s == "Y" ? true : false) : false;
                }
            }
            else
            {
                return false;
            }
        }
    }

    //protected override void Page_Load(object sender, EventArgs e)
    //{

    //}

    //修改
    protected void gridView_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    //增加
    protected void gridView_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        #region  唯一性判断
        using (DataClassesDataContext lqdb = new DataClassesDataContext())
        {
            if (e.NewValues["hymc"] != null)
            {
                string Hymc = e.NewValues["hymc"].ToString(); var result = lqdb.css_hydj.FirstOrDefault(r => r.hymc == Hymc);
                if (result != null)
                    throw new Exception("对不起，会员名称不能重复！");
            }
            if (e.NewValues["hykh"] != null)
            {
                string Hykh = e.NewValues["hykh"].ToString();
                var results = lqdb.css_hydj.FirstOrDefault(r => r.hykh == Hykh);
                if (results != null)
                    throw new Exception("对不起，会员卡号不能重复！");
            }
            if (e.NewValues["sfzh"] != null)
            {
                string Sfzh = e.NewValues["sfzh"].ToString();
                var resultS = lqdb.css_hydj.FirstOrDefault(r => r.sfzh == Sfzh);
                if (resultS != null)
                    throw new Exception("对不起！身份证号已经存在！");
            }
            if (e.NewValues["mobile"] != null)
            {
                string Mobile = e.NewValues["mobile"].ToString();
                var resultM = lqdb.css_hydj.FirstOrDefault(r => r.sfzh == Mobile);
                if (resultM != null)
                    throw new Exception("对不起！手机号码已经存在！");
            }
        }
        #endregion
        //e.NewValues["qsrq"] = ;//会员卡起始日期根据首次发卡时间统计
        e.NewValues["founder"] = CMSUser.Current.ID;
        e.NewValues["fdate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
        e.NewValues["editor"] = CMSUser.Current.ID;
        e.NewValues["edate"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }


    //edit初始化
    protected void gridView_CellEditorInitialize(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewEditorEventArgs e)
    {
        if (e.Column.FieldName == "hybm")
        {
            if (e.Value == null)
            {
                using (DataClassesDataContext lqdb = new DataClassesDataContext())
                {
                    int number = lqdb.css_hydj.Count() + 1;
                    //(e.Editor as ASPxTextBox).Text = number.ToString();
                    (e.Editor as ASPxTextBox).Text = WebUtility.GenerateCode(number.ToString(), 'H', 10);
                }
            }
        }
        if (e.Column.FieldName == "hymc")
        {
            e.Editor.Focus();
        }
        e.Editor.SetClientSideEventHandler("KeyPress", @"function(s,e){var key = event.keyCode;if( key==13){EnterAsTab();}}");
    }

    //ajax客户端获取数据
    //[WebMethod]
    //public static string[] GetInfo(string hybm)
    //{
    //    using (DataClassesDataContext context = new DataClassesDataContext())
    //    {
    //        //var us = context.css_department_rybm.Where(u => u.bmbm.Trim() == hybm.Trim()).Select(u => u.rybm).ToArray();
    //        var Hybm = context.css_hydj_jnr.Where(u => u.hybm.Trim() == hybm.Trim()).Select(u=>u.hybm).ToArray();
    //        var Jnrlx = context.css_hydj_jnr.Where(u => u.hybm.Trim() == hybm.Trim()).Select(u => u.jnrlx).ToArray();
    //        var Jnr = context.css_hydj_jnr.Where(u => u.hybm.Trim() == hybm.Trim()).Select(u => u.jnr).ToArray();

    //        string[] Lists = new string[4];
    //        Lists[0] = bz[0];
    //        Lists[1] = spbm[0];
    //        Lists[2] = flag_sp[0];
    //        Lists[3] = flag_zp[0];
    //        return Lists;
    //    }
    //}
}